package com.arpa.ntocc.basic.api.controller;

import com.arpa.core.web.Result;
import com.arpa.ntocc.basic.api.service.impl.FileUploadService;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.ntocc.common.security.annotation.NoReSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传接口
 * @Author liuyj
 * @Date 2019-12-10 16:20:32
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping(value="/fileUpload")
@Log4j2(topic = "business")
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;

    @ApiOperation(value = "文件上传", notes = "文件上传，可一次性上传多个文件")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @PostMapping("")
    @NeedLogin
    public Result sendVerificationCode(@RequestParam(value="file") MultipartFile[] myFiles) {
        try {
            Result result = fileUploadService.uploadFile(myFiles);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 华为OCR图片识别
     * @param request
     * @return
     */
    @ApiOperation(value = "华为OCR图片识别 -- 暂不用", notes = "华为OCR图片识别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @PostMapping(value = "/OCRDistinguish")
    public Result OCRdistinguish(HttpServletRequest request){
//        try {
//            Map<String, Object> params = ServletsUtil.getParameters(request);
//            String path=(String)params.get("path");
//            String ocrType=request.getParameter("ocrType");
//            Map<String, Object> map = new HashMap<>();
//            try {
//                if(ocrType!=null && !"".equals(ocrType)) {
//                    OCRdistinguish ocr=new OCRdistinguish();
//                    map=ocr.getOCRLicense(ocrType, path);
//                }else {
//                    return new SimpleObjectResult(ReturnCode.ERROR, "识别类型不能为空");
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                System.err.println();
//            }
//            logger.info("map: " + map);
//            return new SimpleObjectResult(ReturnCode.SUCCESS, "恭喜您，识别成功", map);
//        }
//        catch (Exception e) {
//            logger.error("exception", e);
//            return new SimpleObjectResult(ReturnCode.ERROR, "很遗憾，识别失败");
//        }
        return Result.ok("操作成功！");
    }
 }
